<div class="layui-fluid layui-col-md12" lay-title="编辑分类">
  <div class="layui-card">
    <div class="layui-card-body" style="padding: 15px;">
      <form class="layui-form" action="" lay-filter="form-group">
        <input type="hidden" name="cid" style="display:none">
		<input type="hidden" name="cdat" style="display:none">
        <div class="layui-form-item">
          <label class="layui-form-label">分类标题</label>
          <div class="layui-input-block">
            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题(20个字符内)"
              class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">排序</label>
          <div class="layui-input-block">
            <input type="number" name="sort" autocomplete="off" placeholder="1"
              class="layui-input">
          </div>
        </div>		
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="categorys-submit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>		
      </form>
    </div>
  </div>
</div>

<script>
  layui.use(['admin', 'form', 'laydate'], function () {
    var $ = layui.$,
      admin = layui.admin,
      layer = layui.layer,
      router = layui.router(),
      form = layui.form;
    form.render(null, 'form-group');
    // 拉取友链详情
    if (router.search.cid) {
      admin.get({
        url: layui.api.categorys + 'get?cid=' + router.search.cid,
        async: false,
        success: function (result) {
          let res = result.data;
          $('input[name="cid"]').val(res.cid);
          $('input[name="title"]').val(res.title);
		  $('input[name="sort"]').val(res.sort);
		  $('input[name="cdat"]').val(res.cdat);
          form.render(null, 'form-group');
        }
      });
    } else {
      layui.view.setTitle('新增分类', router.href);
    }

    /* 自定义验证规则 */
    form.verify({
      title: function (value) {
        if (value.length < 1) {
          return '标题至少得1个字符啊';
        }
      }
    });

    /* 监听提交 */
    form.on('submit(categorys-submit)', function (data) {
	  /** 屏蔽回车 **/
      var theEvent = window.event; 
      var code = theEvent.keyCode || theEvent.which || theEvent.charCode; 
	  var time = Math.round(new Date().getTime() / 1000).toString();
      if (code == 13) {  
        return false; 
      } 	
      let datas = data.field;
      if (!datas.status) {
        datas.status = 2;
      } else {
        datas.status = 1;
      }
	  if (!router.search.cid) {
          datas.cdat = time;
	  }

      // 这一段是用于判断是否有相同的值的
      datas.onlykey = 'title';
      datas.onlyname = datas.title;
      admin.post({
        url: layui.api.categorys + 'save',
        type: 'post',
        data: datas,
        success: function (result) {
          layer.alert(result.msg, {
            title: '提交提示'
          }, function () {
            // 这里加上时间戳,刷新页面
            admin.navigate('/list/categorys/time=' + time);
          });

        }
      });
      return false;
    });
  });
</script>